package com.party.alipay;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;

public class MyAlipayUtils {

	/**
	 * 支付
	 * 测试接口：127.0.0.1:8080/pay/alipay?out_trade_no=11111111&subject=SpringBoot测试商品&total_amount=0.01&body=测试&product_code=QUICK_WAP_PAY
	 * 
	 * @param out_trade_no
	 *            商户订单号，商户网站订单系统中唯一订单号，必填
	 * @param subject
	 *            订单名称，必填
	 * @param total_amount
	 *            付款金额，必填
	 * @param body
	 *            商品描述，可空
	 * @param timeout_express
	 *            超时时间 可空
	 * @param product_code
	 *            销售产品码 必填QUICK_WAP_PAY
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	public static void alipay(String out_trade_no, String subject, String total_amount, String body,
			String timeout_express, String product_code, HttpServletResponse response, HttpServletRequest request)
			throws Exception {
		// 得到支付宝客户端对象
		AlipayClient alipayClient = AlipayConfig.getAliPayClient();
		// 构造请求
		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
		// 封装请求支付信息
		AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
		model.setOutTradeNo(out_trade_no);
		model.setSubject(subject);
		model.setTotalAmount(total_amount);
		model.setBody(body);
		model.setTimeoutExpress(timeout_express);
		model.setProductCode(product_code);
		alipayRequest.setBizModel(model);

		// 设置异步通知地址
		alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
		// 设置同步地址
		alipayRequest.setReturnUrl(AlipayConfig.return_url);

		String form = "";
		try {
			// 调用SDK生成表单
			form = alipayClient.pageExecute(alipayRequest).getBody();
			response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
			response.getWriter().write(form);// 直接将完整的表单html输出到页面
			response.getWriter().flush();
			response.getWriter().close();
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	/**
	 * 交易查询接口
	 * 
	 * @param WIDout_trade_no
	 *            商户订单号
	 * @param WIDtrade_no
	 *            支付宝交易号
	 * @return
	 * @throws AlipayApiException
	 * @throws IOException 
	 */
	public static AlipayQueryVO query(String WIDout_trade_no, String WIDtrade_no)
			throws AlipayApiException, IOException {
		AlipayClient client = AlipayConfig.getAliPayClient();
		AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

		AlipayTradeQueryModel model = new AlipayTradeQueryModel();
		model.setOutTradeNo(WIDout_trade_no);
		model.setTradeNo(WIDtrade_no);
		alipayRequest.setBizModel(model);
		AlipayTradeQueryResponse alipayResponse = client.execute(alipayRequest);
		//转换成Json对象
		AlipayQueryVO queryVo = JSON.parseObject(alipayResponse.getBody(),AlipayQueryVO.class);
		return queryVo;
	}
	
	
	
	/**
	 * 
	 * @param out_trade_no   	本商户订单号(与支付宝流水号必须要其一，若两者都有，以支付宝为准)
	 * @param trade_no			支付宝流水单号
	 * @param out_request_no	退款单号（唯一标识一次退款）
	 * @param refund_amount		退款金额（不得大于原金额）
	 * @param refund_reason		退款原因
	 * @return
	 * @throws AlipayApiException 
	 */
	public static AlipayRefundVO refund(String out_trade_no,String trade_no,
			String out_request_no,String refund_amount,String refund_reason) throws AlipayApiException{
		
		AlipayClient client = AlipayConfig.getAliPayClient();
		AlipayTradeRefundRequest alipay_request = new AlipayTradeRefundRequest();
		AlipayTradeRefundModel model=new AlipayTradeRefundModel();
		//设置参数
		model.setOutTradeNo(out_trade_no);
		model.setTradeNo(trade_no);
		model.setRefundAmount(refund_amount);
		model.setRefundReason(refund_reason);
		model.setOutRequestNo(out_request_no);
		alipay_request.setBizModel(model);
		//执行请求
		AlipayTradeRefundResponse alipay_response =client.execute(alipay_request);
		AlipayRefundVO alipayRefund = JSON.parseObject(alipay_response.getBody(),AlipayRefundVO.class);
		if(null!=alipayRefund)
			alipayRefund.getAlipay_trade_refund_response().setRefund_reason(refund_reason);
		
		return alipayRefund;
	}

}
